from django.shortcuts import render
from django.http import HttpResponse
from .. import models
# Create your views here.
# 图书商城后台首页
def index(request):
    return render(request,'myadmin/index.html')

# 后台登陆页面
def mylogin(request):
    return render(request,'myadmin/login.html')

# 后台登陆认证
def mydologin(request):
    data = request.POST.dict()
    data.pop('csrfmiddlewaretoken')

    # 检测账号和密码
    obj = models.Users.objects.filter(phone=data['phone'])
    if obj.count() == 0:
        return HttpResponse('<script>alert("账号或密码错误");history.back();</script>')

    # 完成登陆操作
    from django.contrib.auth.hashers import make_password
    if make_password(data['password'],obj[0].password):
        request.session['AdminUser'] = {'id':obj[0].id,'phone':obj[0].phone}
        return HttpResponse('<script>alert("欢迎登陆！！"); location.href="/myadmin"</script>')
    else:
        return HttpResponse('<script>alert("账号或密码错误");history.back();</script>')


